Homework 04: Modeling Language homework For selected problems on our LP-Formulation homework, re-write them using a mathematical-programming language. Run them to make sure you have the syntax and meaning correct. Try to avoid variable names like Production23. Instead use an array of variables like Production[i,j]. That is, declare something like (in AMPL syntax) set Durations; set StartMonths; var Production {d in Durations, s in StartMonths}; then use things like sum {d in Durations} .... ------------------------------ If you want to use AMPL: You can run AMPL models online for free by going to http://www.ampl.com/TRYAMPL/startup.html Take a look at some of their sample files, pick one that's reasonably close to the model you want to do, and make some changes. Save it as a file on your hard drive, then upload it to that service to run it. ------------------------------ Or, if you have a Windows machine, try using the AMPL-like language PuLP (which is Python-based) in http://solverstudio.org/ which also lets you solve bigger problems than the 200-variable, 100-constraint limit in Excel Solver. Note that PuLP only lets you do Linear programs (whether integer or continuous), while Pyomo can express nonlinear programs (but you still need a solver that can solve them, and solverstudio doesn't come with any nonlinear solvers as of Fall 2016). ----------------------------------- Here are two files that show a formulation of Knapsack in AMPL, with the added twist that the data is generated randomly. You can use these: * to see how models get written in AMPL, and * to see how randomness gets included if you want to use that. ------------------------- ## filename: ampl_knapsack_rnd.mod param numitems default 25; param profitscale default 2^5; param weightscale default 10; set Items := 1 .. numitems; # a place to save values before printing them param old_totalprofit default 0; param old_weightbody default 0; param old_x {Items} default 0; var x{Items} binary; # no reason we have to call it "x"; could be called anything. # for more information on using random parameters, see http://ampl.com/BOOK/CHAPTERS/10-params.pdf # and then I'm not sure about this: http://ampl.github.io/amplgsl/randist.html param profit{Items} := round(Exponential()*profitscale); # other options include Normal(mean,variance), or # Uniform(lowerlimit,upperlimit) param weight{Items} := round(Exponential()*weightscale); # On some problems I don't want to use round() ; it depends on the problem # information on round(): http://ampl.com/BOOK/CHAPTERS/14-command.pdf # we'll set the weight limit to be about 50% of the total item weights, # so it will auto-scale. param weightlimit default sum{i in Items} weight[i] / 2; maximize totalprofit: sum {i in Items} profit[i] * x[i]; objective totalprofit; subject to weightconstraint: sum {i in Items} weight[i] * x[i] <= weightlimit; # param data_name symbolic ; ------------------------- ## filename: ampl_knapsack_rnd.run #model ampl_knapsack_rnd.mod; # data ampl_knapsack_rnd.dat; # old stuff: #let ratio := 0.15; #display ratio >> ac13rnd.out ; option randseed loopindex; for {loopindex in 1..3} { # seed the random number generator differently for each loop iteration: #option randseed loopindex; # doing a "reset" on these data will re-randomize them reset data profit, weight ; #option dual_initial_guesses 0; # This is from an old file. I can't remember what this does; we probably don't need it. # let's try solving it twice, once with the integer constraint and once without. option relax_integrality 1; solve; # save the values temporarily let {i in Items} old_x[i] := x[i]; let old_weightbody := weightconstraint.body; # the left-hand side of the constraint; what the actual sum of selected item weights was let old_totalprofit := totalprofit ; # now enforce integrality and re-solve: option relax_integrality 0; solve; # debugging information: we probably don't want to print this out when we're doing long runs, # but here's how we could print it out if we're curious: display profit, weight, old_x, x ; # just the summary information: # if we do it with "display", it ends up with one variable output per line, which is hard for post-processing. #display loopindex weightconstraint.body, weightlimit, totalprofit ; # Instead, we can use print: print "summary:", loopindex, old_weightbody, weightconstraint.body, weightlimit, old_totalprofit, totalprofit ; # print "difference = ", difference >> ac13rnd.out; # display c1,c2, parab_min, old_q, q, old_theta, theta >> ac13rnd.out; # display Y,Max, given_thermal_dual, ot_line_thermal.body, my_line_thermal.body, ot_line_thermal.slack >> ac13rnd.out; } # end of "for {loopindex in 1..3}"